package com.digitalchina.lxnsy.zbsy;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.ConsultDynamicDetailActivity;
import com.digitalchina.lxnsy.api.BusinessConsultDynamic;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ConsultDynamicListVO;
import com.digitalchina.lxnsy.entity.EarManagerListVO;
import com.digitalchina.lxnsy.sy.login.LoginActivity;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.viewholder.ConsultDynamicListHolder;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/2/7 16:27
 * 修改备注：
 * ================================================
 */
public class EarManagerListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;
    protected final int REQUEST_CODE_1 = 111;

    private RecyclerArrayAdapter adapter;

    private int page = 1;
    private int limit = 10;
    private List<EarManagerListVO> consultDynamicListVOList = new ArrayList<>();


    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_consult_dynamic_list);
        setTitle("耳标管理");
        setRightText("新增");
    }

    @Override
    public void initMember() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new EarManagerListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);
                switch (msg.what) {
                    case MSG.MSG_QUERY_CONSULT_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_QUERY_CONSULT_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        consultDynamicListVOList = FastJsonUtil.getListBeanRow(object.toString(), "rows", EarManagerListVO.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(consultDynamicListVOList);

        if (consultDynamicListVOList != null) {
            if (consultDynamicListVOList.size() < limit) {
                adapter.stopMore();
            }
        }

    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoForResultActivity(activity, TagManageAddActivity.class, REQUEST_CODE_1);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            page = 1;
            BusinessConsultDynamic.queryListPage(EarManagerListActivity.this, page, limit, "",mHandler);

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessConsultDynamic.queryListPage(EarManagerListActivity.this, page, limit, "",mHandler);
    }

    @Override
    public void onLoadMore() {
        if (consultDynamicListVOList.size() >= limit) {
            page++;
            BusinessConsultDynamic.queryListPage(EarManagerListActivity.this, page, limit,"", mHandler);
        }
    }
}
